//Base y Core
#include "Base.h"
#include "core.h"

//Managers
#include "GraphicsManager.h"
#include "FontManager.h"
#include "InputManager.h"

//Camaras
#include "Camera.h"
#include "ThPSCamera.h"
#include "FPSCamera.h"

//Para graficar
#include "VertexTypes.h"
#include "RenderableVertexs.h"
#include "2DTexture.h"
#include "Texture.h"

#include "videogameprocess.h"
#include "Frustum.h"




CVideogameProcess::~CVideogameProcess()
{
	int k = 0;
}

void CVideogameProcess::Init()
{
	
	

	m_pObject= new CObject3D(Vect3f(0.0f,1.7f,0.0f)	,0.0f	,0.0f		,0.0f);
	

	Vect2i res=CORE->GetGraphicsManager()->GetScreenRes();
	float ap=(float)(res.x/res.y);
	m_pFPS=new CFPSCamera(0.0f,50.0f,FLOAT_PI_VALUE/2,ap,m_pObject);
	m_pTPS=new CThPSCamera(0.0f,50.0f,FLOAT_PI_VALUE/2,ap,m_pObject,5.0f);
	m_pCamera= m_pTPS;
	


	m_Texture=new C2DTexture();

	m_Texture->Load("./Data/tiger.bmp");

	TCOLORED_VERTEX l_Vrtx[4]=
	{
		{2,8,0,0xffffffff},
		{6,8,0,0xffffffff},
		{2,3,0,0xffffffff},
		{6,3,0,0xffffffff}
	
	};

	TTEXTURED_VERTEX    l_Vtxs2[4] = 
	{
		{12,8,0,0,0},
		{16,8,0,1,0},
		{12,3,0,0,1},
		{16,3,0,1,1}
	};


	unsigned short l_Indxs[6]={0,1,2,2,1,3};
	m_RV=new CIndexedVertexs<TCOLORED_VERTEX>(CORE->GetGraphicsManager(),l_Vrtx,l_Indxs,4,6);
	m_RV2 = new CIndexedVertexs<TTEXTURED_VERTEX>(CORE->GetGraphicsManager(), l_Vtxs2, l_Indxs, 4, 6);
	
}

void CVideogameProcess::Deinit()
{
	CHECKED_DELETE(	m_pObject);
	
	CHECKED_DELETE(	m_pFPS);
	CHECKED_DELETE(	m_pTPS);
	
	CHECKED_DELETE(	m_RV);
	CHECKED_DELETE(	m_RV2);
	CHECKED_DELETE(	m_Texture);
}

void CVideogameProcess::Update(float dt, float fps)
{
	CGraphicsManager*	gm=CORE->GetGraphicsManager();
	CInputManager*		im=CORE->GetInputManager();
	CThPSCamera*		tpc;

	tpc=(CThPSCamera*)m_pCamera;
	
	//modifico angulo segun movimiento del mouse
	Vect3i dm=im->GetMouseDelta();
	Vect3f l_pos=m_pObject->GetPosition();
	
	float l_yaw=m_pObject->GetYaw()-(dm.x*0.01f);
	float l_pitch=m_pObject->GetPitch()+(dm.y*0.01f);

	Vect3f l_dir=Vect3f(cos(l_yaw),0.0f,sin(l_yaw));

	if(im->IsDown(IDV_MOUSE,0))
	{
		m_pObject->SetYaw(l_yaw);
		m_pObject->SetPitch(l_pitch);
	}

	
	//zoom

	tpc->AddZoom(-dm.z*0.01f);

	//toggle cam
	
	if(im->IsUpDown(IDV_KEYBOARD,KEY_T)) m_CamToggle=!m_CamToggle;
	if(m_CamToggle) 
	{
		m_pFPS->SetObject3D(m_pObject);
		m_pTPS->SetObject3D(m_pObject);
	}
	else 
	{
		
	}
		

	//-------------------------------------------

	//toggle cam view

	if(im->IsUpDown(IDV_KEYBOARD, KEY_V)) m_ViewToggle=!m_ViewToggle;
	if(m_ViewToggle) m_pCamera=m_pTPS;
	else m_pCamera= m_pFPS;

	//------------------------------------------
	

	//modifico velocidad segun la tecla shift
	if(im->IsDown(IDV_KEYBOARD,KEY_LSHIFT)) m_speed=4.0f;
	else m_speed=2.0f;
	//-----------------------------------------

	//movimiento del personaje
		//avance
	if(im->IsDown(IDV_KEYBOARD,KEY_W))		m_pObject->SetPosition(l_pos+(l_dir*m_speed*dt));
	if(im->IsDown(IDV_KEYBOARD,KEY_S))		m_pObject->SetPosition(l_pos-(l_dir*m_speed*dt));
		//strafe
	l_dir=l_dir^Vect3f(0.0,1.0,0.0);
	
	if(im->IsDown(IDV_KEYBOARD,KEY_D))		m_pObject->SetPosition(l_pos-(l_dir*m_speed*dt));
	if(im->IsDown(IDV_KEYBOARD,KEY_A))		m_pObject->SetPosition(l_pos+(l_dir*m_speed*dt));
	//-------------------------------------------------------------



}

void CVideogameProcess::RenderScene()
{
	Mat44f l_matrix;
	

	
	m_Texture->Activate(0);
	m_RV->Render(CORE->GetGraphicsManager());
	m_RV2->Render(CORE->GetGraphicsManager());
	l_matrix.SetIdentity();
	CORE->GetGraphicsManager()->SetTransform(l_matrix);

	//CORE->GetGraphicsManager()->DrawAxis(5.0);
	CORE->GetGraphicsManager()->DrawGrid(20.0,20,colWHITE);


	


}

void CVideogameProcess::RenderScene2D()
{
	CORE->GetFontManager()->DrawDefaultText(100,100,colWHITE,"Sup World!?");
	CORE->GetFontManager()->DrawLiteral(100,115,"HiWorld");
	int id_font = CORE->GetFontManager()->GetTTF_Id("arial");
	CORE->GetFontManager()->DrawText(100,130,colWHITE,id_font,"Sup World!? en arial");
	
}

uint32		CVideogameProcess::RenderDebugInfo		()
{
	uint32 l_yaccum;
	CFontManager* fm=CORE->GetFontManager();



	l_yaccum=CProcess::RenderDebugInfo();
	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Yaw = %.2f",m_pObject->GetYaw());
	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Pos_x = %.2f",m_pObject->GetPosition().x);
	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Pos_z = %.2f",m_pObject->GetPosition().z);
	
	

	return l_yaccum;

}


//TCOLORED_VERTEX